// JavaScript Document
//variables
var geocoder;
var map;
//          
function initialize() {
    //coder analysis
    geocoder = new google.maps.Geocoder();
    var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
    var myOptions = {
        zoom: 4,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

    // We create pathCoordinates as an MVCArray so we can
    // manipulate it using the insertAt() method
    pathCoordinates = new google.maps.MVCArray();

    var polyOptions = {
        path: pathCoordinates,
        strokeColor: '#000000',
        strokeOpacity: 1.0,
        strokeWeight: 3
    }
    poly = new google.maps.Polyline(polyOptions);
    poly.setMap(map);

    google.maps.event.addListener(map, 'click', function(event) {
        placeMarker(event);
    });
}

function placeMarker(event) {
    //Icon png
    var meIcon = 'image/yuwei.png';
    var path = poly.getPath();

    // Because path is an MVCArray, we can simply append a new coordinate
    // and it will automatically appear
    path.insertAt(pathCoordinates.length, event.latLng);
                
    var marker = new google.maps.Marker({
        position: event.latLng,
        map: map,
        icon: meIcon
    });
    map.setCenter(event.latLng);
    showInfoWindowMarker(marker,event);
}

function showInfoWindowMarker(marker, event){
    //var point = marker.projectionController.fromLatLngToDivPixel(event.latLng);
    //var overlayProjection = this.getProjection();
    //var of = overlayProjection.fromLatLngToDivPixel(event.latLng);
    //RouteOverlay.prototype = new google.maps.OverlayView();
    
    //var mcp = new google.maps.MapCanvasProjection();
    var point = map.fromLatLngToDivPixel(event.latLng);
    var message = 'Lat:'+ event.latLng.lat() + ',' + event.latLng.lng() + '<br/>'
        + 'Pixel:'+ point.x + ',' + point.y;
    var infowindow = new google.maps.InfoWindow({
        content: message
    })
    google.maps.event.addListener(marker, 'click', function(){
        infowindow.open(map,marker);
    })
}
            
function codeAddress() {
    var address = document.getElementById("address").value;
    if (geocoder) {
        geocoder.geocode( {
            'address': address
        }, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                map.setCenter(results[0].geometry.location);
                var marker = new google.maps.Marker({
                    map: map,
                    position: results[0].geometry.location
                });
            } else {
                alert("Geocode was not successful for the following reason: " + status);
            }
        });
    }
}

